import Vue from 'vue'
import App from '../App'
import Router from 'vue-router'
const Home =()=> import('../page/Home')
const Login =()=> import('../page/Login')
const zlhwList =()=> import('../components/zlhwList')
const addArticle =()=> import('../components/addArticle')
const userList =()=> import('../components/userList')
const whiteList =()=> import('../components/whiteList')
const columnList =()=> import('../components/columnList')
const zlhwReadyList =()=> import('../components/zlhwReadyList')

Vue.use(Router)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router =  new Router({
  routes: [
    {path: '/', component: App,redirect: '/Home'},
    {path: '/Login', name: 'Login', component: Login},
    {path:'/Home',name:'Home',component:Home,redirect:{path:'/zlhwReadyList'},children:[
        {path:'/zlhwList',name:'zlhwList',component:zlhwList},
        {path:'/addArticle/:type',name:'addArticle',component:addArticle},
        {path:'/userList',name:'userList',component:userList},
        {path:'/whiteList',name:'whiteList',component:whiteList},
        {path:'/columnList',name:'columnList',component:columnList},
        {path:'/zlhwReadyList',name:'zlhwReadyList',component:zlhwReadyList}
        ]}
  ]
})

//挂载路由守卫函数
router.beforeEach((to, from, next) => {
  if (to.path === '/Login') return next();
  const tokenStr = window.sessionStorage.getItem('userId');
  if (!tokenStr) next('/Login');
  next();
})

export default router
